/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2010 huliqing, huliqing.cn@gmail.com
 *
 * This file is part of QFaces.
 * QFaces is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * QFaces is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with QFaces.  If not, see <http://www.gnu.org/licenses/>.
 *
 * 这个文件是QFaces的一部分。
 * 您可以单独使用或分发这个文件，但请不要移除这个头部声明信息.
 * QFaces是一个自由软件，您可以自由分发、修改其中的源代码或者重新发布它，
 * 新的任何修改后的重新发布版必须同样在遵守LGPL3或更后续的版本协议下发布.
 * 关于LGPL协议的细则请参考COPYING、COPYING.LESSER文件，
 * 您可以在QFaces的相关目录中获得LGPL协议的副本，
 * 如果没有找到，请连接到 http://www.gnu.org/licenses/ 查看并获取副本。
 *
 * - Author: Huliqing
 * - Contact: huliqing.cn@gmail.com
 * - License: GNU Lesser General Public License (LGPL)
 * - Blog and source code availability: http://www.huliqing.name/
 */

package name.huliqing.qfaces.component;

//import java.util.logging.Logger;
import javax.el.MethodExpression;
import javax.el.ValueExpression;
import javax.faces.component.UIComponentBase;
import javax.faces.context.FacesContext;
import name.huliqing.qfaces.QFaces;
import name.huliqing.qfaces.model.PageModel;
import name.huliqing.qfaces.model.PageParam;

/**
 * 
 * @author huliqing
 *
 */
public class Scroller extends UIComponentBase {
//	private final static Logger logger = Logger.getLogger(Scroller.class.getName());

    // ---- 主要的控件参数:
    // 组件绑定的dataTable的id
    private String _for;
    // 方法绑定，用于从Bean中获取data
    private MethodExpression listener;
    // 请参考UIData的var属性
    private String var;
    // [top,bottom,both] 将scroller显示在上部还是下部,默认下部
    private String display;
    // [Number]显示的页面数,数字类型
    private Integer displayPage;
    // [true,false]是否显示总记录数,默认true
    private Boolean displayCount;
    // [true,false]是否通过input进行跳转,默认true,通过在input中输入页面跳转
    private Boolean displayJump;
    // [true,false]是否通过select框进行跳转,默认false
    private Boolean displayJumpBySelect;
    // 每页显示数量
    private Integer pageSize;
    // "总记录数"的文字描述
    private String strRecordTotal;
    // "总页数"的文字描述
    private String strPageTotal;
    // "跳转"按钮的文字描述
    private String strJump;
    // 是否渲染组件,如果false,则不渲染翻页导航条，看起来没有一点作用。
    // 实际上这可以与t:dataTable（MyFaces)配合使用，组件为t:dataTable提供分页数据，
    // 页t:scroller渲染分页导航,因为q:scroller渲染的分页条是固定位置的，可能存在不够自由的地方
    private Boolean visible;
    // 用于排序的字段，格式像这样： “field1,field2,field3”,即通过“，”号分隔
    private String sortFields;
    // Scroller 的CSS样式
    private String style;
    // New “上一页”的图片路径
    private String srcPagePrevious;
    private String srcPageNext;
    private String srcPageFirst;
    private String srcPageLast;
    // 翻页栏的样式
    private String styleClassMouseOn;
    private String styleClassMouseOut;
    private String styleClassCurrent;
    // 翻页图片的外div的样式,(鼠标响应时的样式)
    private String styleClassImageMouseOn;
    private String styleClassImageMouseOut;
    // 外观样式0,1...默认0
    private String face;
    // 用于存储起始记录,起始记录优先从parameter中的CONS_RECORD_START中获取,
    // 如果parameter中没有CONS_RECORD_START,再从recordStart中恢复,如果仍然
    // 没有,则设为0,这样可以方便用户通过URL直接进行传递，而不影响页数
    private Integer recordStart;
    // 当前的排序字段
    private String sortField;
    // 是否是倒序的。
    private Boolean asc;
    // 排序图标“正序”
    private String srcAsc;
    // 排序图标“倒序”
    private String srcDesc;

    private String loadType;

    // 翻页模式,可为 post/get，默认：post, 在有表单提交的情况下需要用到post翻页模式
    private String method;

    // ----------------------
    public Scroller() {
        super();
        setRendererType("name.huliqing.qfaces.render.ScrollerRenderer");
    }

    @Override
    public String getFamily() {
        return "name.huliqing.qfaces.render.ScrollerFamily";
    }
    private Object[] _values;

    @Override
    public void restoreState(FacesContext fc, Object state) {
        _values = (Object[]) state;
        super.restoreState(fc, _values[0]);
        this._for = (String) _values[1];
        this.listener = (MethodExpression) _values[2];
        this.var = (String) _values[3];
        this.display = (String) _values[4];
        this.displayPage = (Integer) _values[5];
        this.displayCount = (Boolean) _values[6];
        this.displayJump = (Boolean) _values[7];
        this.displayJumpBySelect = (Boolean) _values[8];
        this.pageSize = (Integer) _values[9];
        this.strRecordTotal = (String) _values[10];
        this.strPageTotal = (String) _values[11];
        this.strJump = (String) _values[12];
        this.visible = (Boolean) _values[13];
        this.sortFields = (String) _values[14];
        this.style = (String) _values[15];
        this.srcPagePrevious = (String) _values[16];
        this.srcPageNext = (String) _values[17];
        this.srcPageFirst = (String) _values[18];
        this.srcPageLast = (String) _values[19];
        this.styleClassMouseOn = (String) _values[20];
        this.styleClassMouseOut = (String) _values[21];
        this.styleClassCurrent = (String) _values[22];
        this.styleClassImageMouseOn = (String) _values[23];
        this.styleClassImageMouseOut = (String) _values[24];
        this.face = (String) _values[25];
        this.recordStart = (Integer) _values[26];
        this.sortField = (String) _values[27];
        this.asc = (Boolean) _values[28];
        this.srcAsc = (String) _values[29];
        this.srcDesc = (String) _values[30];
        this.loadType = (String) _values[31];
        this.method = (String) _values[32];
    }

    @Override
    public Object saveState(FacesContext fc) {
        if (_values == null) {
            _values = new Object[33];
        }
        _values[0] = super.saveState(fc);
        _values[1] = this._for;
        _values[2] = this.listener;
        _values[3] = this.var;
        _values[4] = this.display;
        _values[5] = this.displayPage;
        _values[6] = this.displayCount;
        _values[7] = this.displayJump;
        _values[8] = this.displayJumpBySelect;
        _values[9] = this.pageSize;
        _values[10] = this.strRecordTotal;
        _values[11] = this.strPageTotal;
        _values[12] = this.strJump;
        _values[13] = this.visible;
        _values[14] = this.sortFields;
        _values[15] = this.style;
        _values[16] = this.srcPagePrevious;
        _values[17] = this.srcPageNext;
        _values[18] = this.srcPageFirst;
        _values[19] = this.srcPageLast;
        _values[20] = this.styleClassMouseOn;
        _values[21] = this.styleClassMouseOut;
        _values[22] = this.styleClassCurrent;
        _values[23] = this.styleClassImageMouseOn;
        _values[24] = this.styleClassImageMouseOut;
        _values[25] = this.face;
        _values[26] = this.recordStart;
        _values[27] = this.sortField;
        _values[28] = this.asc;
        _values[29] = this.srcAsc;
        _values[30] = this.srcDesc;
        _values[31] = this.loadType;
        _values[32] = this.method;
        return _values;
    }

    private Object getFromExp(String name) {
        ValueExpression _ve = getValueExpression(name);
        if (_ve != null) {
            return _ve.getValue(getFacesContext().getELContext());
        } else {
            return null;
        }
    }

    public MethodExpression getListener() {
        return listener;
    }

    public void setListener(MethodExpression listener) {
        this.listener = listener;
    }

    /**
     * 通过方法表达式设置listener绑定,所绑定的方法具有如下签名:e.g.<BR />
     * PageModel<?> loadData(PageParam pp);
     * @see #setListener(MethodExpression)
     * @param methodExpression
     * 		方法表达式，格式如：#{WebBean.loadData}
     */
    public void setListenerAsExpression(String methodExpression) {
        MethodExpression me = QFaces.createMethodExpression(
                methodExpression, PageModel.class, new Class[]{PageParam.class});
        if (me != null) {
            this.setListener(me);
        }
    }

    public String getFor() {
        if (null != this._for) {
            return this._for;
        }
        return (String) this.getFromExp("for");
    }

    public void setFor(String _for) {
        this._for = _for;
    }

    public String getVar() {
        if (null != this.var) {
            return this.var;
        }
        return (String) this.getFromExp("var");
    }

    public void setVar(String var) {
        this.var = var;
    }

    public String getDisplay() {
        if (null != this.display) {
            return this.display;
        }
        return (String) this.getFromExp("display");
    }

    public void setDisplay(String display) {
        this.display = display;
    }

    public Integer getDisplayPage() {
        if (null != this.displayPage) {
            return this.displayPage;
        }
        return (Integer) this.getFromExp("displayPage");
    }

    public void setDisplayPage(Integer displayPage) {
        this.displayPage = displayPage;
    }

    public Boolean getDisplayCount() {
        if (null != this.displayCount) {
            return this.displayCount;
        }
        return (Boolean) this.getFromExp("displayCount");
    }

    public void setDisplayCount(Boolean displayCount) {
        this.displayCount = displayCount;
    }

    public Boolean getDisplayJump() {
        if (null != this.displayJump) {
            return this.displayJump;
        }
        return (Boolean) this.getFromExp("displayJump");
    }

    public void setDisplayJump(Boolean displayJump) {
        this.displayJump = displayJump;
    }

    public Boolean getDisplayJumpBySelect() {
        if (null != this.displayJumpBySelect) {
            return this.displayJumpBySelect;
        }
        return (Boolean) this.getFromExp("displayJumpBySelect");
    }

    public void setDisplayJumpBySelect(Boolean displayJumpBySelect) {
        this.displayJumpBySelect = displayJumpBySelect;
    }

    public Integer getPageSize() {
        if (null != this.pageSize) {
            return this.pageSize;
        }
        return (Integer) this.getFromExp("pageSize");
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public String getStrRecordTotal() {
        if (null != this.strRecordTotal) {
            return this.strRecordTotal;
        }
        return (String) this.getFromExp("strRecordTotal");
    }

    public void setStrRecordTotal(String strRecordTotal) {
        this.strRecordTotal = strRecordTotal;
    }

    public String getStrPageTotal() {
        if (null != this.strPageTotal) {
            return this.strPageTotal;
        }
        return (String) this.getFromExp("strPageTotal");
    }

    public void setStrPageTotal(String strPageTotal) {
        this.strPageTotal = strPageTotal;
    }

    public String getStrJump() {
        if (null != this.strJump) {
            return this.strJump;
        }
        return (String) this.getFromExp("strJump");
    }

    public void setStrJump(String strJump) {
        this.strJump = strJump;
    }

    public Boolean getVisible() {
        if (null != this.visible) {
            return this.visible;
        }
        return (Boolean) this.getFromExp("visible");
    }

    public void setVisible(Boolean visible) {
        this.visible = visible;
    }

    public String getSortFields() {
        if (null != this.sortFields) {
            return this.sortFields;
        }
        return (String) this.getFromExp("sortFields");
    }

    public void setSortFields(String sortFields) {
        this.sortFields = sortFields;
    }

    public String getStyle() {
        if (null != this.style) {
            return this.style;
        }
        return (String) this.getFromExp("style");
    }

    public void setStyle(String style) {
        this.style = style;
    }

    public String getSrcPagePrevious() {
        if (null != this.srcPagePrevious) {
            return this.srcPagePrevious;
        }
        return (String) this.getFromExp("srcPagePrevious");
    }

    public void setSrcPagePrevious(String srcPagePrevious) {
        this.srcPagePrevious = srcPagePrevious;
    }

    public String getSrcPageNext() {
        if (null != this.srcPageNext) {
            return this.srcPageNext;
        }
        return (String) this.getFromExp("srcPageNext");
    }

    public void setSrcPageNext(String srcPageNext) {
        this.srcPageNext = srcPageNext;
    }

    public String getSrcPageFirst() {
        if (null != this.srcPageFirst) {
            return this.srcPageFirst;
        }
        return (String) this.getFromExp("srcPageFirst");
    }

    public void setSrcPageFirst(String srcPageFirst) {
        this.srcPageFirst = srcPageFirst;
    }

    public String getSrcPageLast() {
        if (null != this.srcPageLast) {
            return this.srcPageLast;
        }
        return (String) this.getFromExp("srcPageLast");
    }

    public void setSrcPageLast(String srcPageLast) {
        this.srcPageLast = srcPageLast;
    }

    public String getStyleClassMouseOn() {
        if (null != this.styleClassMouseOn) {
            return this.styleClassMouseOn;
        }
        return (String) this.getFromExp("styleClassMouseOn");
    }

    public void setStyleClassMouseOn(String styleClassMouseOn) {
        this.styleClassMouseOn = styleClassMouseOn;
    }

    public String getStyleClassMouseOut() {
        if (null != this.styleClassMouseOut) {
            return this.styleClassMouseOut;
        }
        return (String) this.getFromExp("styleClassMouseOut");
    }

    public void setStyleClassMouseOut(String styleClassMouseOut) {
        this.styleClassMouseOut = styleClassMouseOut;
    }

    public String getStyleClassCurrent() {
        if (null != this.styleClassCurrent) {
            return this.styleClassCurrent;
        }
        return (String) this.getFromExp("styleClassCurrent");
    }

    public void setStyleClassCurrent(String styleClassCurrent) {
        this.styleClassCurrent = styleClassCurrent;
    }

    public String getStyleClassImageMouseOn() {
        if (null != this.styleClassImageMouseOn) {
            return this.styleClassImageMouseOn;
        }
        return (String) this.getFromExp("styleClassImageMouseOn");
    }

    public void setStyleClassImageMouseOn(String styleClassImageMouseOn) {
        this.styleClassImageMouseOn = styleClassImageMouseOn;
    }

    public String getStyleClassImageMouseOut() {
        if (null != this.styleClassImageMouseOut) {
            return this.styleClassImageMouseOut;
        }
        return (String) this.getFromExp("styleClassImageMouseOut");
    }

    public void setStyleClassImageMouseOut(String styleClassImageMouseOut) {
        this.styleClassImageMouseOut = styleClassImageMouseOut;
    }

    public String getFace() {
        if (null != this.face) {
            return this.face;
        }
        return (String) this.getFromExp("face");
    }

    public void setFace(String face) {
        this.face = face;
    }

    public String getSrcAsc() {
        if (this.srcAsc != null) {
            return this.srcAsc;
        }
        return (String) this.getFromExp("srcAsc");
    }

    public void setSrcAsc(String srcAsc) {
        this.srcAsc = srcAsc;
    }

    public String getSrcDesc() {
        if (this.srcDesc != null) {
            return this.srcDesc;
        }
        return (String) this.getFromExp("srcDesc");
    }

    public void setSrcDesc(String srcDesc) {
        this.srcDesc = srcDesc;
    }

    public String getLoadType() {
        if (loadType != null) {
            return loadType;
        }
        return (String) this.getFromExp("loadType");
    }

    public void setLoadType(String loadType) {
        this.loadType = loadType;
    }

    public String getMethod() {
        if (method != null) {
            return method;
        }
        return (String) this.getFromExp("method");
    }

    public void setMethod(String method) {
        this.method = method;
    }

    // ---- 以下变量不应该手动设置，这是自动计算出来的
    public Integer getRecordStart() {
        return this.recordStart;
    }

    public void setRecordStart(Integer recordStart) {
        this.recordStart = recordStart;
    }

    public String getSortField() {
        return this.sortField;
    }

    public void setSortField(String sortField) {
        this.sortField = sortField;
    }

    public Boolean getAsc() {
        return this.asc;
    }

    public void setAsc(Boolean asc) {
        this.asc = asc;
    }

    // ---- 临时变量，不作状态保存
    // 总记录数
    private Integer recordTotal;
    // 总页数
    private Integer pageTotal;
    // 当前页
    private Integer pageCurrent;

    public Integer getRecordTotal() {
        return recordTotal;
    }

    public void setRecordTotal(Integer recordTotal) {
        this.recordTotal = recordTotal;
    }

    public Integer getPageTotal() {
        return pageTotal;
    }

    public void setPageTotal(Integer pageTotal) {
        this.pageTotal = pageTotal;
    }

    public Integer getPageCurrent() {
        return pageCurrent;
    }

    public void setPageCurrent(Integer pageCurrent) {
        this.pageCurrent = pageCurrent;
    }


}
